package com.pxxy.app_homepage.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.pxxy.app_homepage.entity.Information;
import com.pxxy.lib_common.entity.User;
import com.pxxy.security512.R;

import java.util.List;

public class FragmentHomeRecommendRecyclerAdapter extends RecyclerView.Adapter<FragmentHomeRecommendRecyclerAdapter.RecommendHolder> {
    private Context context;
    private List<Information> informationList;
    private ItemListener itemListener;


    public FragmentHomeRecommendRecyclerAdapter(Context context, List<Information> informationList) {
        this.context = context;
        this.informationList = informationList;
    }

    public void setItemListener(ItemListener itemListener) {
        this.itemListener = itemListener;
    }

    @NonNull
    @Override
    public RecommendHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.homepage_fragment_home_recommend_recycler_item, viewGroup, false);
        return new RecommendHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendHolder recommendHolder, int i) {

        if (informationList.get(i).getAddress().size() < 3) {
            //显示第一种布局，隐藏第二种布局
            recommendHolder.oneRelativeLayout.setVisibility(View.VISIBLE);
            recommendHolder.twoRelativeLayout.setVisibility(View.GONE);
            ///适配数据
            recommendHolder.title.setText(informationList.get(i).getTitle());
            recommendHolder.user.setText(informationList.get(i).getAuthor().getUserMessage().getNickName());
            recommendHolder.see.setText(informationList.get(i).getSee() + "");
            //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
            RequestOptions options = new RequestOptions()
                    .transform(new CenterCrop(), new RoundedCorners(10))
                    .override(180, 140);
            //加载图片
            Glide.with(context).load(informationList.get(i).getAddress().get(0)).apply(options).into(recommendHolder.image);

            if (informationList.get(i).getComments() != null) {
                recommendHolder.comment.setText(informationList.get(i).getComments().size() + "评论");
            } else {
                recommendHolder.comment.setText("0评论");
            }
        }else {
            //显示第二种布局，隐藏第一种布局
            recommendHolder.oneRelativeLayout.setVisibility(View.GONE);
            recommendHolder.twoRelativeLayout.setVisibility(View.VISIBLE);
            //适配数据
            recommendHolder.twoTitle.setText(informationList.get(i).getTitle());
            recommendHolder.twoUser.setText(informationList.get(i).getAuthor().getUserMessage().getNickName());
            recommendHolder.twoSee.setText(informationList.get(i).getSee()+"");
            if (informationList.get(i).getComments() != null) {
                recommendHolder.twoComment.setText(informationList.get(i).getComments().size()+"评论");
            } else {
                recommendHolder.twoComment.setText("0评论");
            }

            //设置采样率
            RequestOptions options = new RequestOptions()
                    .override(180, 140);
            //加载图片
            Glide.with(context).load(informationList.get(i).getAddress().get(0)).apply(options).into(recommendHolder.twoImage1);
            Glide.with(context).load(informationList.get(i).getAddress().get(1)).apply(options).into(recommendHolder.twoImage2);
            Glide.with(context).load(informationList.get(i).getAddress().get(2)).apply(options).into(recommendHolder.twoImage3);
        }

    }

    @Override
    public int getItemCount() {
        return informationList.size();
    }

    //ViewHolder缓存类
    class RecommendHolder extends RecyclerView.ViewHolder {

        ImageView image,twoImage1,twoImage2,twoImage3;
        TextView title, user, see, comment,twoTitle,twoUser,twoComment,twoSee;
        RelativeLayout relativeLayout,twoRelativeLayout,oneRelativeLayout;

        public RecommendHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
            initEvent();
        }

        private void initEvent() {
            //设置item的点击事件
            if (itemListener != null) {
                relativeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        itemListener.itemListener(v, getLayoutPosition(), informationList.get(getLayoutPosition()));
                        title.setTextColor(context.getResources().getColor(R.color.fragment_home_recommend_recycler_title_select));
                        twoTitle.setTextColor(context.getResources().getColor(R.color.fragment_home_recommend_recycler_title_select));
                    }
                });
            }
        }

        private void initView(View itemView) {
            //第一种布局
            image = itemView.findViewById(R.id.fragment_home_recommend_recycler_iv);
            title = itemView.findViewById(R.id.fragment_home_recommend_recycler_title_tv);
            user = itemView.findViewById(R.id.fragment_home_recommend_recycler_user_tv);
            see = itemView.findViewById(R.id.fragment_home_recommend_recycler_see_tv);
            relativeLayout = itemView.findViewById(R.id.fragment_home_recommend_recycler_rl);
            comment = itemView.findViewById(R.id.fragment_home_recommend_recycler_comment);
            oneRelativeLayout = itemView.findViewById(R.id.fragment_home_recommend_recycler_one_rl);

            //第二种布局
            twoTitle = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_title_tv);
            twoComment = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_comment);
            twoImage1 = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_iv1);
            twoImage2 = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_iv2);
            twoImage3 = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_iv3);
            twoUser = itemView.findViewById(R.id.fragment_home_recommend_recycler_user_two_tv);
            twoSee = itemView.findViewById(R.id.fragment_home_recommend_recycler_see_two_tv);
            twoRelativeLayout = itemView.findViewById(R.id.fragment_home_recommend_recycler_two_rl);

        }

    }

    /**
     * 事件监听
     */
    public interface ItemListener {
        void itemListener(View view, int position, Information info);
    }
}
